﻿
$(function () {
    init();
}); //end $()

//页面JS初始化
function init() {
    $("#divHtml").on("click", function (event) {
        var obj = $(event.target);
        var name = obj.attr("name");
        switch (name) {
            case "search"://列表搜索
                search(obj);
                break;
            case "callResult"://通话记录切换
                callerTimeSpanDivShoe(obj);
                break;
            case "btnReset"://筛选条件重置
                resetSearch();
                break;
            case "callLog"://加载通话记录
                if (obj.attr("data-flag") == "true") {
                    loadCallLog(obj);
                }
                break;
        }
        $("#callResult").on("change", function () {
            var obj = $(this);
            callerTimeSpanDivShoe(obj);
        })
    });//end divHtml click
};

/*
* 加载通话记录
*/
function loadCallLog(obj) {
    var keyId = obj.attr("data-id");
    var sourceId = obj.attr("data-sourceId");
    var requestData = {
        KeyId: keyId,
        SourceKeyId: sourceId
    };
    var ChannelInquirySource = obj.parents("tr").children(".source").text();
    $.AjaxAgency("/Customer/GetWashCustomerCallInfo", requestData, function (data) {
        if (data.Flag) {
            if (data.Result != null && data.Result.length > 0) {
                var divContentHtml = "<div id='ChannelDetaildiv' class='build-name-select-div'>";
                divContentHtml += "<table class='data-table data-table-loose'><tr><th>来源</th><th>通话时间</th><th>通话时长</th><th>录音</th><th>下载</th></tr>";
                for (var i = 0; i < data.Result.length; i++) {
                    var date = "";
                    if (data.Result[i].StartTime) {
                        date = new Date(parseInt(data.Result[i].StartTime.replace(/\D/igm, ""))).toLocaleString().replace(/:\d{1,2}$/, ' ');
                    }
                    if (data.Result[i].RecordingUrl != null && data.Result[i].RecordingUrl != "") {
                        divContentHtml += "<tr><td>" + ChannelInquirySource + "</td><td>" + date + "</td><td>" + data.Result[i].CallerTimespan + "</td><td>" + player.replace(/{url}/, data.Result[i].RecordingUrl) + "</td><td><a href='" + data.Result[i].RecordingUrl + "'>下载</a></td></tr>";
                    }
                    else {
                        divContentHtml += "<tr><td>" + ChannelInquirySource + "</td><td>" + date + "</td><td>" + data.Result[i].CallerTimespan + "</td><td><a>无</a></td><td><a>无</a></td></tr>";
                    }
                }

                divContentHtml += "</table></div>";
                $.dialog({
                    id: "BuildNameSelect",
                    title: "通话记录",
                    lock: true,
                    content: divContentHtml
                });
            } else {
                $.OKDialogAgency('<div class="tc">通话记录为空</div>');
            }
        } else {
            $.OKDialogAgency('<div class="tc">获取失败</div>');
        }
    })
}

/*
* 通话时长下拉框控制
*/
function callerTimeSpanDivShoe(obj) {
    var value = obj.val();
    if (value == "0") {
        //显示通话时长下拉框
        $("#callerTimeSpanDiv").show();
    } else {
        //隐藏通话时长下拉框
        $("#callerTimeSpanDiv").hide();
        //通话时长默认选中第一个
        $("#callerTimeSpan option:first").prop("selected", 'selected');
    }
};


/*
 * 列表搜索按钮
 */
function search(obj) {
    $("#hidWashSearch").click();
};

/*
* 筛选条件重置
*/
function resetSearch() {
    $("#WashSourctType option:first").prop("selected", 'selected');
    $("#callResult option:first").prop("selected", 'selected');
    $("#callerTimeSpanDiv").hide();
    $("#callerTimeSpan option:first").prop("selected", 'selected');
    $("#txtStartTime").val("");
    $("#txtEndTime").val("");
    $("#txtfindDepartName").val("");
    $("#txtfindDepartKey").val("");
    $("#txtfindEmployeeName").val("");
    $("#txtfindEmployeeKey").val("");
};

function demo(obj) {
    console.log("demo");
};

var player = '<audio controls="" src="{url}"></audio>';
if (navigator.appName.indexOf("Microsoft Internet Explorer") != -1) { //ie下播放器
    player = '<object width="36" height="33" id="Player" classid="CLSid:6BF52A52-394A-11d3-B153-00C04F79FAA6" type="application/x-oleobject" border="0"><PARAM NAME="URL" VALUE="{url}"><PARAM NAME="rate" VALUE="1"><PARAM NAME="balance" VALUE="0"><PARAM NAME="currentPosition" VALUE="0"><PARAM NAME="defaultFrame" VALUE=""><PARAM NAME="playCount" VALUE="1"><PARAM NAME="autoStart" VALUE="0"><PARAM NAME="currentMarker" VALUE="0"><PARAM NAME="invokeURLs" VALUE="-1"><PARAM NAME="baseURL" VALUE=""><PARAM NAME="volume" VALUE="50"><PARAM NAME="mute" VALUE="0"><PARAM NAME="uiMode" VALUE="full"><PARAM NAME="stretchToFit" VALUE="-1"><PARAM NAME="windowlessVideo" VALUE="0"><PARAM NAME="enabled" VALUE="-1"><PARAM NAME="enableContextMenu" VALUE="0"><PARAM NAME="fullScreen" VALUE="0"><PARAM NAME="SAMIStyle" VALUE=""><PARAM NAME="SAMILang" VALUE=""><PARAM NAME="SAMIFilename" VALUE=""><PARAM NAME="captioningID" VALUE=""><PARAM NAME="enableErrorDialogs" VALUE="0"><PARAM NAME="_cx" VALUE="2646"><PARAM NAME="_cy" VALUE="794"></object>';
}